//
//  WDJPublicData.m
//  wdj
//
//  Created by 蓝鳍互娱 on 2024/7/8.
//

#import "WDJPublicData.h"

@implementation WDJPublicData
static WDJPublicData *defaultManager = nil;
+(WDJPublicData *)defaultManager{
    static dispatch_once_t token;
    dispatch_once(&token, ^{
        if(defaultManager == nil){
            defaultManager = [[self alloc] init];
        }
    });
    return defaultManager;
}
- (void)updataPublicData{
    if (![WDJPublicData defaultManager].worldDayTitleDic||![WDJPublicData defaultManager].worldDayTime) {
        [WDJPublicData defaultManager].worldDayTitleDic = [[NSUserDefaults standardUserDefaults] objectForKey:@"worldDayTitle"];
        [WDJPublicData defaultManager].worldDayTime = [[NSUserDefaults standardUserDefaults] objectForKey:@"worldDayTime"];
        [WDJPublicData defaultManager].lunPans = [[NSUserDefaults standardUserDefaults] objectForKey:@"lunPanTitle"];
    }
    if (![[WDJPublicData defaultManager].worldDayTime isEqualToString:[self getCurrentTime]]) {
        [self changePublicData];
    }
    [self updataLunPanData];
}
- (void)updataLunPanData{
    if (![WDJPublicData defaultManager].lunPans) {
        NSArray *lunPans = @[@"一起打台球",@"小吃街吃一圈",@"一起去爬山",@"看个展览",@"学习做陶艺",@"看个电影",@"去游乐场玩"];
        [WDJPublicData defaultManager].lunPans = lunPans;
        [[NSUserDefaults standardUserDefaults] setObject:[WDJPublicData defaultManager].lunPans forKey:@"lunPanTitle"];
    }
}
- (void)changePublicData{
    NSString *worldDayFilePath = [[NSBundle mainBundle] pathForResource:@"wdjWorldDay" ofType:@"plist"];
    NSArray *worldDays = [NSArray arrayWithContentsOfFile:worldDayFilePath];
    int worldDayCount = arc4random() % worldDays.count;
    [WDJPublicData defaultManager].worldDayTitleDic = worldDays[worldDayCount];
    [WDJPublicData defaultManager].worldDayTime = [self getCurrentTime];
    [[NSUserDefaults standardUserDefaults] setObject:[WDJPublicData defaultManager].worldDayTitleDic forKey:@"worldDayTitle"];
    [[NSUserDefaults standardUserDefaults] setObject:[WDJPublicData defaultManager].worldDayTime forKey:@"worldDayTime"];
}
- (NSString *)getCurrentTime{
    NSDateFormatter *format = [[NSDateFormatter alloc] init];
    format.dateFormat = @"yyyy-MM-dd";
    NSString *currentTime = [format stringFromDate:[NSDate date]];
    return currentTime;
}
@end
